package com.ruoyi.equ.service.impl;

import com.ruoyi.common.utils.uuid.IdUtils;

import com.ruoyi.equ.domain.EquBasic;
import com.ruoyi.equ.domain.EquColReruReport;
import com.ruoyi.equ.domain.EquColRetuDetail;
import com.ruoyi.equ.domain.EquColRetuRecording;
import com.ruoyi.equ.mapper.EquBasicMapper;
import com.ruoyi.equ.mapper.EquColRetuDetailMapper;
import com.ruoyi.equ.mapper.EquColRetuRecordingMapper;
import com.ruoyi.equ.service.IEquColRetuRecordingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * 设备出入库记录信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-06-22
 */
@Service
public class EquColRetuRecordingServiceImpl implements IEquColRetuRecordingService
{
    @Autowired(required = false)
    private EquColRetuRecordingMapper equColRetuRecordingMapper;

    @Autowired(required = false)
    private EquColRetuDetailMapper equColRetuDetailMapper;

    @Autowired(required = false)
    private EquBasicMapper equBasicMapper;



    /**
     * 查询设备出入库记录信息
     * 
     * @param id 设备出入库记录信息ID
     * @return 设备出入库记录信息
     */
    @Override
    public EquColRetuRecording selectEquColRetuRecordingById(String id)
    {
        EquColRetuRecording equColRetuRecording =  equColRetuRecordingMapper.selectEquColRetuRecordingById(id);
        //获取明细
        EquColRetuDetail equColRetuDetail = new EquColRetuDetail();
        equColRetuDetail.setColRetId(id);
        List<EquColRetuDetail> list = equColRetuDetailMapper.selectEquColRetuDetailList(equColRetuDetail);
        equColRetuRecording.setList(list);
        return equColRetuRecording;
    }

    /**
     * 查询设备出入库记录信息列表
     * 
     * @param equColRetuRecording 设备出入库记录信息
     * @return 设备出入库记录信息
     */
    @Override
    public List<EquColRetuRecording> selectEquColRetuRecordingList(EquColRetuRecording equColRetuRecording)
    {
        return equColRetuRecordingMapper.selectEquColRetuRecordingList(equColRetuRecording);
    }

    /**
     * 根据设备号查询设备领用记录信息
     *
     * @param nums
     * @param typeLabel
     */
    @Override
    public List<EquColReruReport> selectEquColRetuRecordingByNumAndType(String nums, String typeLabel) {
        return equColRetuRecordingMapper.selectEquColRetuRecordingByNumAndType(nums, typeLabel);
    }


    /**
     * 根据设备编号查询设备领用信息
     * @param equNum
     * @return
     */
    @Override
    public List<EquColRetuRecording> selectEquColRetuRecordingByEquNum(String equNum) {
        return equColRetuRecordingMapper.selectEquColRetuRecordingByEquNum(equNum);
    }

    /**
     * 新增设备出入库记录信息
     * 
     * @param equColRetuRecording 设备出入库记录信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertEquColRetuRecording(EquColRetuRecording equColRetuRecording)
    {
        String id = IdUtils.fastSimpleUUID();
        equColRetuRecording.setId(id);
        //新增明细
        if(equColRetuRecording.getList().size() >0) {
            //设备ids
            List<String> ids = new ArrayList<>(equColRetuRecording.getList().size());
            for(EquColRetuDetail equColRetuDetail : equColRetuRecording.getList()) {
                equColRetuDetail.setId(IdUtils.fastSimpleUUID());
                equColRetuDetail.setColRetId(id);
                ids.add(equColRetuDetail.getEquId());
            }
            equBasicMapper.updateEquBasics("2", ids.toArray(new String[0]));
            equColRetuDetailMapper.batchColRetuDetail(equColRetuRecording.getList());
        }
        //变更检测单状态
        //entrustmentAgreementService.updateEntrustmentStateByNum("3", equColRetuRecording.getNums());
        return equColRetuRecordingMapper.insertEquColRetuRecording(equColRetuRecording);
    }

    /**
     * 修改设备出入库记录信息
     * 
     * @param equColRetuRecording 设备出入库记录信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateEquColRetuRecording(EquColRetuRecording equColRetuRecording)
    {
        //删除原本数据，添加新明细记录
        int ret = equColRetuDetailMapper.deleteEquColRetuDetailByRId(equColRetuRecording.getId());
        if(ret > 0 && equColRetuRecording.getList().size() > 0) {
            //设备ids
            List<String> ids = new ArrayList<>(equColRetuRecording.getList().size());
            for(EquColRetuDetail equColRetuDetail: equColRetuRecording.getList()) {
                equColRetuDetail.setId(IdUtils.fastSimpleUUID());
                equColRetuDetail.setColRetId(equColRetuRecording.getId());
                ids.add(equColRetuDetail.getEquId());
            }
            equBasicMapper.updateEquBasics("1", ids.toArray(new String[0]));
            equColRetuDetailMapper.batchColRetuDetail(equColRetuRecording.getList());
        }
        return equColRetuRecordingMapper.updateEquColRetuRecording(equColRetuRecording);
    }

    /**
     * 根据单号更新信息
     * @param equColRetuRecording 设备出入库记录信息
     * @return
     */
    @Override
    public int updateEquColRetuRecordingByNums(EquColRetuRecording equColRetuRecording) {
        return equColRetuRecordingMapper.updateEquColRetuRecordingByNums(equColRetuRecording);
    }

    /**
     * 根据委托单号删除出入库记录
     * @param nums 设备领用委托单号
     * @return
     */
    @Override
    public int deleteEquColRetuRecordingByNums(String nums) {
        return equColRetuRecordingMapper.deleteEquColRetuRecordingByNums(nums);
    }

    /**
     * 批量删除设备出入库记录信息
     * 
     * @param ids 需要删除的设备出入库记录信息ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteEquColRetuRecordingByIds(String[] ids)
    {
        //删除入库单
        int ret = equColRetuRecordingMapper.deleteEquColRetuRecordingByIds(ids);
        if(ret > 0) {
            for(int i = 0 ; i < ids.length; i++) {
                //获取设备明细信息
                EquColRetuDetail equColRetuDetail = new EquColRetuDetail();
                equColRetuDetail.setColRetId(ids[i]);
                List<EquColRetuDetail> equColRetuDetailList = equColRetuDetailMapper.selectEquColRetuDetailList(equColRetuDetail);
                if(equColRetuDetailList.size() > 0) {
                    //获取设备唯一性编号并变更
                    for(EquColRetuDetail detail : equColRetuDetailList) {
                        EquBasic equBasic = new EquBasic();
                        equBasic.setEquNum(detail.getEquNum());
                        equBasic.setState("1");
                        equBasicMapper.updateEquBasicByNum(equBasic);
                    }
                }
                equColRetuDetailMapper.deleteEquColRetuDetailByRId(ids[i]);
            }
        }
        return ret;
    }

    /**
     * 删除设备出入库记录信息信息
     * 
     * @param id 设备出入库记录信息ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteEquColRetuRecordingById(String id)
    {
        //获取设备明细信息
        EquColRetuDetail equColRetuDetail = new EquColRetuDetail();
        equColRetuDetail.setColRetId(id);
        List<EquColRetuDetail> equColRetuDetailList = equColRetuDetailMapper.selectEquColRetuDetailList(equColRetuDetail);
        if(equColRetuDetailList.size() > 0) {
            //获取设备唯一性编号并变更
            for(EquColRetuDetail detail : equColRetuDetailList) {
                EquBasic equBasic = new EquBasic();
                equBasic.setEquNum(detail.getEquNum());
                equBasic.setState("1");
                equBasicMapper.updateEquBasicByNum(equBasic);
            }
        }
        equColRetuDetailMapper.deleteEquColRetuDetailByRId(id);
        return equColRetuRecordingMapper.deleteEquColRetuRecordingById(id);
    }
}
